﻿package top.yangyitao.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.context.ContextLoader;

import top.yangyitao.model.JsonChess;
import top.yangyitao.model.Point;
import top.yangyitao.service.AIService;
import top.yangyitao.util.MachineLearningUtil;
import top.yangyitao.util.MonteCarlo;

@WebServlet("/AIonStepServlet")
public class AIonStepServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;   

    public AIonStepServlet() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	    /**
	     * jsonChessArray 落在棋盘上的棋子的向量 
	     * 每一个元素包含x，y还有flag（黑/白棋子）
	     * 在数组中的位置代表为第几颗落子
	     */
		
		request.setCharacterEncoding("UTF-8");
		int AIFlag = Integer.parseInt(request.getParameter("AIFlag"));
		int userFlag = Integer.parseInt(request.getParameter("userFlag"));
		int chessNum = Integer.parseInt(request.getParameter("chessNum"));
		String chessArrayJsonString = request.getParameter("chessArray");
		String[] chessArray = chessArrayJsonString.substring(1, chessArrayJsonString.length()-1).split("}");
		JsonChess[] jsonChessArray = new JsonChess[chessNum];
		int[][] chessBoard = new int[15][15];
		for(int i=0;i<chessNum;i++) {
			String chessJson;
			if(i>0)
				chessJson = chessArray[i].substring(2);
			else
				chessJson = chessArray[i].substring(1);
			jsonChessArray[i] = new JsonChess(chessJson);
			chessBoard[jsonChessArray[i].getY()][jsonChessArray[i].getX()] = jsonChessArray[i].getFlag();
		}
		int x,y;
		x = jsonChessArray[chessNum-1].getX();
		y = jsonChessArray[chessNum-1].getY();
		Point p = new Point(x,y);
		AIService aIService = (AIService)ContextLoader.getCurrentWebApplicationContext().getBean("aIService");
		int win = aIService.isWin(chessBoard, p);
		if(win==0) {
			p = MonteCarlo.getBestPoint(chessBoard, AIFlag, userFlag);
			x = p.getX();
			y = p.getY();
			chessBoard[y][x] = AIFlag;
			win = aIService.isWin(chessBoard, p);
		}
		else {
			/**
			 * 用户赢了
			 * 
			 * 整个棋局的信息都在chessBoard中
			 * 棋子序列在chessArray中
			 * 0代表无子
			 * AIFlag代表是AI的棋子
			 * userFlag代表是用户的棋子
			 * 
			 * 此处应加入学习代码
			 * 
			 */
			aIService.saveKnowlage(MachineLearningUtil.SaveLearnArea(chessBoard, p,jsonChessArray));
		}
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		String jsonResultString = "{"+"'x':"+p.getX()+","+"'y':"+p.getY()+","+"'win':"+win+"}";
		out.print(jsonResultString);
		out.flush();
		out.close();
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
